package itsm.isperp.module.service.db;

import itsm.isperp.framework.data.commons.repository.BaseRepository;
import itsm.isperp.framework.data.domain.FilterRule;
import itsm.isperp.framework.service.BaseService;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.db.DbEntityField;
import itsm.isperp.module.entity.db.DbEntityFieldControl;
import itsm.isperp.module.repository.db.DbEntityFieldMapper;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DbEntityFieldService extends BaseService<DbEntityField> {

	@Autowired
	protected DbEntityFieldMapper dbEntityFieldMapper;

	@Autowired
	protected DbEntityFieldControlService dbEntityFieldControlService;

	@Override
	public BaseRepository<DbEntityField> getRepository() {
		return this.dbEntityFieldMapper;
	}

	public void saveOrUpdate(DbEntityField entity,
			DbEntityFieldControl controlEntity, boolean haveControl) {

		int result = this.saveOrUpdate(entity);
		if (haveControl && result > 0) {
			controlEntity.setDbEntityFieldId(entity.getId());
			this.dbEntityFieldControlService.saveOrUpdate(controlEntity);
		} else {
			this.dbEntityFieldControlService.deleteByFieldId(entity.getId());
		}

	}

	/**
	 * 根据实体名称获取所有的属性集合
	 * 
	 * @param entityName
	 * @return
	 */
	public List<DbEntityField> findAllByEntityName(String entityName) {

		return dbEntityFieldMapper.findAllByEntityName(entityName);
	}

	public List<DbEntityField> findAllByTableName(String tableName) {

		return dbEntityFieldMapper.findAllByTableName(tableName);
	}

	/**
	 * 根据实体名称获取所有的属性集合
	 * 
	 * @param entityName
	 * @return
	 */
	public List<DbEntityField> findAllByEntityId(String id) {
		return findAllByEntityId(id, null);
	}

	/**
	 * 根据实体名称获取所有的属性集合
	 * 
	 * @param entityName
	 * @return
	 */
	public List<DbEntityField> findAllByEntityId(String id, String[] columns,
			FilterRule... rules) {
		JqGridRequest req = new JqGridRequest();
		req.isPageable(false);
		if (columns != null)
			req.setSelectColumns(columns);
		req.addFilterRule("db_entity_id", JqGridFilterRuleOp.eq, id);
		if (rules != null) {
			for (FilterRule r : rules) {
				req.addFilterRule(r);
			}
		}
		return dbEntityFieldMapper.findAllByRequest(req);
	}

	public List<Map<String, Object>> findAllByEntityTablename(String tableName,
			String[] columns, FilterRule... rules) {
		JqGridRequest req = new JqGridRequest();
		req.isPageable(false);
		if (columns != null)
			req.setSelectColumns(columns);
		req.addFilterRule("table_name", JqGridFilterRuleOp.eq, tableName);
		if (rules != null) {
			for (FilterRule r : rules) {
				req.addFilterRule(r);
			}
		}
		return dbEntityFieldMapper.findAllDetailByRequest(req);
	}

	public DbEntityFieldControl findFieldControl(String tableName,
			String columnName) {
		return dbEntityFieldControlService.findByTableAndColumn(tableName,
				columnName);
	}

	public String getFieldControlDatasourceSql(DbEntityFieldControl control) {
		return dbEntityFieldControlService.getDatasourceSql(control);
	}

	/**
	 * 获取实体字段的控件定义
	 * 
	 * @param id
	 * @return
	 */
	public DbEntityFieldControl findFieldControl(String dbFieldid) {
		return dbEntityFieldControlService.findByFieldId(dbFieldid);
	}

	/**
	 * 根据实体id删除其定义的字段
	 * 
	 * @param entityId
	 *            实体id
	 */
	public void deleteByEntityId(String entityId) {
		this.dbEntityFieldControlService.deleteByEntityId(entityId);
		this.dbEntityFieldMapper.deleteByEntityId(entityId);

	}

	public DbEntityField findOneByTableNameAndColumn(String tableName,
			String columnName) {
		return this.dbEntityFieldMapper.findOneByTableNameAndColumn(tableName,
				columnName);
	}

}